package com.nidu.demo.web.system;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.permission.api.SysPermissionService;
import com.nidu.demo.user.dto.UserRoleAssignCmd;
import com.nidu.demo.user.api.SysUserService;
import com.nidu.demo.user.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * 用户管理 Controller
 */
@Tag(name = "管理后台 - 用户")
@RestController
@RequestMapping("/system/user")
@Validated
@RequiredArgsConstructor
public class SysUserController {

    private final SysUserService service;

    private final SysPermissionService permissionService;

    @Operation(summary = "创建用户", description = "创建新的用户")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('system:user:create')")
    public SingleResponse<UserCO> addUser(@RequestBody UserAddCmd cmd) {
        return service.add(cmd);
    }

    @Operation(summary = "更新用户", description = "更新用户的信息")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:user:update')")
    public SingleResponse<UserCO> updateUser(@PathVariable Long id, @RequestBody UserUpdateCmd cmd) {
        cmd.setId(id);
        return service.update(cmd);
    }

    @Operation(summary = "删除用户", description = "根据 ID 删除用户")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:user:delete')")
    public Response deleteUser(@PathVariable Long id) {
        return service.delete(id);
    }

    @Operation(summary = "批量删除用户", description = "根据 ID 列表删除多个用户")
    @PostMapping("/batch")
    @PreAuthorize("@ss.hasPermission('system:user:delete')")
    public Response deleteUsers(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return service.delete(new HashSet<>(ids));
    }

    @Operation(summary = "获取用户详情", description = "根据 ID 获取用户详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:user:query')")
    public SingleResponse<UserCO> getUserById(@PathVariable Long id) {
        return service.getById(id);
    }

    @Operation(summary = "查询用户列表", description = "获取所有用户信息")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermission('system:user:query')")
    public MultiResponse<UserCO> listUsers(UserListQry qry) {
        return service.list(qry);
    }

    @Operation(summary = "分页查询用户", description = "分页获取用户信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:user:query')")
    public PageResponse<UserCO> pageUsers(UserPageQry qry) {
        return service.page(qry);
    }

    @Operation(summary = "重置密码", description = "重置用户密码")
    @PostMapping("/reset-password")
    @PreAuthorize("@ss.hasPermission('system:user:reset-password')")
    public Response resetPassword(@RequestBody UserResetPasswordCmd cmd) {
        return service.resetPassword(cmd);
    }

    @Operation(summary = "更新用户状态", description = "更新用户的启用/禁用状态")
    @PutMapping("/{id}/status")
    @PreAuthorize("@ss.hasPermission('system:user:update')")
    public Response updateStatus(@PathVariable Long id, @RequestParam StatusEnum status) {
        return service.updateStatus(id, status);
    }

    @Operation(summary = "查询用户角色ID列表", description = "查询用户角色ID列表")
    @GetMapping("/{userId}/roles")
    public MultiResponse<Long> getUserRole(@PathVariable Long userId) {
        return permissionService.getUserRoles(userId);
    }

    @Operation(summary = "分配用户角色", description = "给用户分配角色")
    @PostMapping("/{userId}/roles")
    public Response assignUserRole(@PathVariable Long userId, @RequestBody UserRoleAssignCmd cmd) {
        cmd.setUserId(userId);
        return permissionService.assignUserRole(cmd);
    }

}